//
//  WH_ClassListCell.m
//  App_6.23
//
//  Created by wanghao on 16/7/4.
//  Copyright © 2016年 wanghao. All rights reserved.
//

#import "WH_ClassListCell.h"
#import "WH_Classification.h"
#import "AppDelegate.h"

#define ROWHEIGHT (100*HEI_ONE)

@interface WH_ClassListCell ()

@property (nonatomic, strong) UIImageView *productImage;
@property (nonatomic, strong) UILabel *productName;
@property (nonatomic, strong) UILabel *rateLabel;
@property (nonatomic, strong) UILabel *numLabel;
@property (nonatomic, strong) UILabel *countLabel;

@end
@implementation WH_ClassListCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self createSubs];
    }
    
    return self;
 
    
}

-(void)createSubs{
    
    self.productImage = [[UIImageView alloc] init];
    [self.contentView addSubview:_productImage];
    [self.productImage mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.mas_equalTo(@0);
        make.width.height.mas_equalTo(@(ROWHEIGHT));
    }];
    
    self.productName = [UILabel new];
    self.productName.font = [UIFont systemFontOfSize:13.f];
    self.productName.numberOfLines = 2;
    [self.contentView addSubview:_productName];
    [self.productName mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.productImage.mas_right).offset(5);
        make.top.equalTo(@10);
        make.right.mas_equalTo(@-70);
        make.height.equalTo(@(35*HEI_ONE));
    }];

    UILabel *labelA = [UILabel new];
    labelA.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1];
    labelA.layer.cornerRadius = 3;
    labelA.layer.masksToBounds = YES;
    [self.contentView addSubview:labelA];
    [labelA mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.productName);
        make.top.equalTo(self.productName.mas_bottom).offset(10);
        make.width.equalTo(self.productName);
        make.height.mas_equalTo(7);
        
    }];
    self.rateLabel = [UILabel new];
    _rateLabel.backgroundColor = [UIColor colorWithRed:0.99 green:0.79 blue:0.27 alpha:1];
    _rateLabel.layer.cornerRadius = 3;
    _rateLabel.layer.masksToBounds = YES;
    [self.contentView addSubview:_rateLabel];
    [self.rateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.height.equalTo(labelA);
        make.width.mas_equalTo(@(28*WID_ONE));
        
    }];

    UIButton *addOrderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //addOrderBtn.backgroundColor = ARCDOM_4;
    [addOrderBtn addTarget:self action:@selector(addOrder:) forControlEvents:UIControlEventTouchUpInside];
    [addOrderBtn setTitle:@"加入清单" forState:UIControlStateNormal];
    [addOrderBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    addOrderBtn.layer.cornerRadius = 3;
    addOrderBtn.layer.borderWidth = .3;
    addOrderBtn.layer.borderColor = [UIColor redColor].CGColor;
    addOrderBtn.titleLabel.font = [UIFont systemFontOfSize:11.f];
    [self.contentView addSubview:addOrderBtn];
    [addOrderBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(@-3);
        make.height.mas_equalTo(25);
        make.left.equalTo(labelA.mas_right).offset(10);
        make.centerY.equalTo(self);
    }];
    
    self.numLabel = [UILabel new];
    self.numLabel.text = @"总需1343";
    self.numLabel.textColor = [UIColor lightGrayColor];
    [self.contentView addSubview:_numLabel];
    [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(labelA.mas_bottom).offset(10);
        make.left.equalTo(labelA);
        make.width.equalTo(@(100));
        
    }];
    
    self.countLabel = [UILabel new];
    self.countLabel.textAlignment = NSTextAlignmentRight;
    self.countLabel.text = @"剩余343";
    self.countLabel.textColor = [UIColor lightGrayColor];
    [self.contentView addSubview:_countLabel];
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(labelA.mas_bottom).offset(10);
        make.right.equalTo(labelA);
        make.height.equalTo(self.numLabel);
        make.width.equalTo(self.numLabel);
    }];

    
    self.numLabel.font = [UIFont systemFontOfSize:12.f];
    self.countLabel.font = [UIFont systemFontOfSize:12.f];

}
-(void)setListC:(WH_Classification *)listC{
    
    if (_listC != listC) {
        
        _listC = listC;
    }
    
    self.productName.text = self.listC.product_name;
    [self.productImage sd_setImageWithURL:[NSURL URLWithString:self.listC.product_image] placeholderImage:PLACEHOLDER_CELL_SAME];
    self.numLabel.text = [NSString stringWithFormat:@"总需%@",self.listC.product_total];
    NSInteger c = [self.listC.product_total integerValue] - [self.listC.product_count integerValue];
    self.countLabel.text = [NSString stringWithFormat:@"剩余%ld",c];
    
    [WH_AppTools changeColor:self.countLabel.text color:[UIColor blueColor] xx:[NSString stringWithFormat:@"%ld",c] nn:self.countLabel];
    //进度条
    CGFloat x = SCREEN_WIDTH - ROWHEIGHT - 75*HEI_ONE;
    CGFloat a = (CGFloat)[self.listC.product_count integerValue]/[self.listC.product_total integerValue]*x;
    NSLog(@"%f",a);
    [self.rateLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.width.equalTo(@(a));
    }];
}


-(void)addOrder:(UIButton *)btn{
    
    if ([ISLOGIN isEqualToString:@"1"]) {
        
        
        NSString *body =[NSString stringWithFormat:@"action=cart_add&user_id=%@&product_period=%@&product_num=1", NSUSERID,self.listC.product_period];
        [WH_AppTools postUrl:URLstr body:body result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
            NSDictionary *dic = result;
            NSString *status = [dic objectForKey:@"status"];
            NSString *message = [dic objectForKey:@"message"];
            if ([status isEqualToString:@"success"]) {
                
                [WSProgressHUD showImage:nil status:@"addCart" maskType:WSProgressHUDMaskTypeClear];

            }else{
                
                [WSProgressHUD showImage:nil status:message maskType:WSProgressHUDMaskTypeClear];
                
            }
            
        } failure:^(NSError *error) {
            
            
        }];

    }else{
        
        [self toLogin];
    }

    
    
}

- (void)toLogin{
    
    //UIApplication是系统单例,可以直接使用
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    
    appDelegate.tabbarMain.selectedIndex = 3;
    
}

- (void)awakeFromNib {
    // Initialization code
    
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
